/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import java.util.*;
import java.io.IOException;
import java.util.Arrays;
import dbhandler.DBConnector;
import java.security.NoSuchAlgorithmException;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.lang.String;

/**
 *
 * @author Tokino
 */
public class Unlocker {

    private String uusername;
    private Hashtable errors;
    private String status;
    private boolean IsTrue;

    public Unlocker() {

        uusername = "";
        status = "";
        IsTrue = false;
        errors = new Hashtable();

    }

    public boolean checkunlocker() throws ClassNotFoundException, SQLException {
        boolean bool = true;
        DBConnector dbConnectorchk = null;
        dbConnectorchk = new DBConnector("localhost", 3306, "selector", "53l3ct0r", "bookaholic");

        System.out.println("wrror me at checkunlocker before if");
        if (uusername.equals("") || uusername.length() < 6 || !(uusername.matches("(?=^.{6,}$)(?=.*[a-zA-Z])(?=.*[\\d])(?!.*[\\W_\\x7B-\\xFF]).*$"))) {
            errors.put("uusername", "Please enter valid a username");
            errors.put("status", "Failure");
            uusername = "";
            bool = false;
        } else if (dbConnectorchk.checkusername(uusername)) {
            errors.put("uusername", "username does not exist");
            errors.put("status", "Failure");
            uusername = "";
            bool = false;
        } else if (dbConnectorchk.checkstatus(uusername)) {
            errors.put("uusername", "username is already active");
            errors.put("status", "Failure");
            uusername = "";
            bool = false;
        }

        System.out.println("wrror me at checkunlocker before bool");
        if (bool == true) {

            CallableStatement callable = null;
            ResultSet result = null;
            dbConnectorchk.connectToDB();
            String authCall = "{ call activate_status(?) }";
            callable = dbConnectorchk.getConnection().prepareCall(authCall);
            callable.setString(1, uusername);
            result = callable.executeQuery();
            errors.put("status", "Success! " + uusername + " was activated");
        }
        System.out.println("wrror me at checkunlocker after bool");
        // dbConnectorchk.disconnectDB();
        return bool;
    }

    public boolean isIsTrue() {
        return IsTrue;
    }

    public void setIsTrue(boolean IsTrue) {
        this.IsTrue = IsTrue;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUusername() {
        return uusername;
    }

    public void setUusername(String uusername) {
        this.uusername = uusername;
    }

    public void setErrors(String key, String msg) {
        errors.put(key, msg);
    }

    public String getErrorMsg(String s) {
        String errorMsg = (String) errors.get(s.trim());
        return (errorMsg == null) ? "" : errorMsg;
    }
}
